package com.rock.code.common.comm;

import java.sql.*;
import java.util.ResourceBundle;

@SuppressWarnings("static-access")
public class ConnectionPool {
    private static int iDrivertype = 0;
    private static String sUserName;
    private static String sPassword;
    private static String sUrl;
    private static String sClassforName;

    private Connection conn = null;
    private boolean bCommitstate;

    static {
        try {
            ResourceBundle jdbc = ResourceBundle.getBundle("jdbc");
            // DES3 t = new DES3();
            sClassforName = jdbc.getString("jdbc.driverClass");
            sUrl = jdbc.getString("jdbc.jdbcUrl");
            sUserName = jdbc.getString("jdbc.user");
            String password = jdbc.getString("jdbc.password");
            // sPassword = t.decrypt(password);
            sPassword = password;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Connection Connection() throws SQLException {
        return conn;
    }

    public Connection createConnection() throws SQLException {
        try {
            Class.forName(sClassforName).newInstance();
            conn = DriverManager.getConnection(sUrl, sUserName, sPassword);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return conn;
    }

    public void closeConn() throws SQLException {
        if (conn != null) {
            conn.close();
            conn = null;
        }
    }

    protected void finalize() throws Throwable {
        if (conn != null) {
            conn.close();
        }
    }

    public Statement createStatement() throws SQLException {
        if (conn != null) {
            return conn.createStatement();
        } else {
            return null;
        }
    }

    public Statement createScrollStatement() throws SQLException {
        if (conn != null) {
            if (iDrivertype == 0) {
                return conn.createStatement();
            } else {
                return conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            }
        } else
            return null;
    }

    public PreparedStatement createPreparedStatement(String sql) throws SQLException {
        if (conn != null) {
            return conn.prepareStatement(sql);
        } else {
            return null;
        }
    }

    public void executePreparedStmt(PreparedStatement pstmt) throws SQLException {
        if (conn != null) {
            pstmt.executeUpdate();
        } else {
            return;
        }
    }

    public CallableStatement createPreparedCall(String sql) throws SQLException {
        if (conn != null) {
            return conn.prepareCall(sql);
        } else {
            return null;
        }

    }

    public void beginTransaction() throws SQLException {
        if (conn != null) {
            bCommitstate = conn.getAutoCommit();
            conn.setAutoCommit(false);
        }
    }

    public void endTransaction() throws SQLException {

        if (conn != null) {
            if (bCommitstate != conn.getAutoCommit()) {
                conn.setAutoCommit(bCommitstate);
            }
        }
    }

    public void commit() throws SQLException {
        if (conn != null) {
            conn.commit();
        }
    }

    public void rollback() throws SQLException {
        if (conn != null) {
            conn.rollback();
        }
    }

    public static void main(String[] argv) throws Exception {
        // String SQL =
        // "select T.TABLE_NAME, T.COLUMN_NAME, T.DATA_TYPE, T.DATA_LENGTH, C.COMMENTS"
        // +
        // " from USER_TAB_COLUMNS T, USER_COL_COMMENTS C where T.TABLE_NAME = C.TABLE_NAME"
        // +
        // " and T.COLUMN_NAME = C.COLUMN_NAME and T.TABLE_NAME = 'TB_SYS_USER'";
        String SQL = "select * from TB_SYS_USER";
        ConnectionPool conPool = new ConnectionPool();
        conPool.createConnection();
        Statement stmt = conPool.createStatement();
        stmt.execute(SQL);
        ResultSet rs;
        rs = stmt.getResultSet();
        ResultSetMetaData resultSetMetaData = rs.getMetaData();
        for (int i = 1; i <= resultSetMetaData.getColumnCount(); i++) {
            System.out.println("*******************************************");
            System.out.println(resultSetMetaData.getColumnName(i));
            System.out.println(resultSetMetaData.getColumnType(i));
            System.out.println(resultSetMetaData.getCatalogName(i));
            System.out.println(resultSetMetaData.getColumnClassName(i));
            System.out.println(resultSetMetaData.getColumnLabel(i));
            System.out.println(resultSetMetaData.getColumnTypeName(i));
            System.out.println(resultSetMetaData.getScale(i));
            System.out.println(resultSetMetaData.getPrecision(i));
        }
        rs.close();
        stmt.close();
        conPool.closeConn();
    }

}
